package StudyCode.summary.mycollection;

import java.util.ArrayList;
import java.util.List;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class GetAsEnumTypeCollection {
	public static final String VALUE_DELIMITER = ",";

	@SuppressWarnings({"rawtypes", "unchecked"})
	public static final <E extends Enum<E>> List getAsEnumTypeCollection(
			String valueAsString, Class<E> type) throws Exception {
		List result = new ArrayList();
		String[] valuesArray = null;

		if ((valueAsString != null) && (!valueAsString.isEmpty())
				&& (type != null)) {
			valuesArray = valueAsString.split(VALUE_DELIMITER);
			for (int i = 0; i < valuesArray.length; i++) {
				result.add(getAsEnumType(valuesArray[i], type));
			}
		}

		return result;
	}

	public static final <E extends Enum<E>> Object getAsEnumType(
			String valueAsString, Class<E> type) throws Exception {
		Object result = null;

		if ((valueAsString != null) && (!valueAsString.isEmpty())
				&& (type != null)) {
			result = Enum.valueOf(type, valueAsString);
		}

		return result;
	}
}
